//
//  SearchViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchSingerViewController.h"
#import "SearchSongViewController.h"
#import "SongModel.h"

@interface SearchViewController ()<UISearchBarDelegate, UIScrollViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) UISearchBar *searchbar;
@property (nonatomic, strong) UISegmentedControl *segmentControl;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) SearchSingerViewController *searchSinger;
@property (nonatomic, strong) SearchSongViewController *searchSong;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x9C9C9C);

    self.automaticallyAdjustsScrollViewInsets = NO;
    
     self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    [self creatSearchBar];
    [self createSegmentControl];
    [self createScrollView];
    [self createImgView];
    self.segmentControl.hidden = YES;
    self.scrollView.hidden = YES;
    
    self.definesPresentationContext = YES;

}
- (void)createImgView{
    self.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"content-empty"]];
    self.imgView.contentMode = UIViewContentModeScaleAspectFit;
    self.imgView.frame = self.scrollView.frame;
    [self.view addSubview:self.imgView];
}
#pragma mark - 搜索
- (void) creatSearchBar{
    self.searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(80, 0, 200, 50)];
    self.searchbar.delegate = self;
    [self.searchbar setTintColor:[UIColor redColor]];
    self.searchbar.alpha = 0.5;
    self.searchbar.placeholder = @"请输入.....";
    [self.searchbar setShowsCancelButton:NO];
    
    [self.searchbar sizeToFit];
    UITextField *searchField = [self.searchbar valueForKey:@"searchField"];
    
    if (searchField) {
        [searchField setBackgroundColor:[UIColor colorWithRed:0.974 green:1.000 blue:0.937 alpha:1.0]];
        [self.navigationController.navigationBar addSubview:self.searchbar];
    }
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    searchText = [searchText stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![searchText isEqualToString:@""]) {
        
        self.imgView.hidden = YES;
        
        NSString *str = [searchText stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        self.segmentControl.hidden = NO;
        self.scrollView.hidden = NO;
        
        NSLog(@"%@", str);
        self.searchSong.searchText = str;
        self.searchSinger.searchText = str;
    }
}
#pragma mark - 创建分段控制器
- (void) createSegmentControl{
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"歌曲", @"歌手"]];
    self.segmentControl.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
    [self.segmentControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:(UIControlEventValueChanged)];
    self.segmentControl.selectedSegmentIndex = 0;
    [self.view addSubview:self.segmentControl];
}
#pragma mark - 创建scrollView
- (void) createScrollView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 104, self.view.bounds.size.width, self.view.bounds.size.height - 40 - 70 - 64)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    
    [self.view addSubview:self.scrollView];
    
    
    self.scrollView.contentSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width * 2, self.view.bounds.size.height - 40 - 70 - 64);
    
    self.searchSinger = [[SearchSingerViewController alloc] init];
    self.searchSong = [[SearchSongViewController alloc] init] ;
    self.searchSinger.navi = self.navigationController;
    self.searchSinger.searchBar = self.searchbar;
    self.searchSong.searchBar = self.searchbar;
    [self addChildViewController:self.searchSinger];
    [self addChildViewController:self.searchSong];
    
    self.searchSong.view.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
    self.searchSinger.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
    [self.scrollView addSubview:self.searchSong.view];
    [self.scrollView addSubview:self.searchSinger.view];
    
    self.scrollView.delegate = self;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    [self.searchbar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchbar resignFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.searchbar removeFromSuperview];
    [self.searchbar resignFirstResponder];
}

#pragma mark - 分段控制器点击方法


- (void)segmentedControlAction:(UISegmentedControl *)sender
{
    [self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.scrollView.frame.size.width, 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.segmentControl.selectedSegmentIndex = n;
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
